package com.afishing.servlet;


import com.afishing.dao.CartDao;
import com.afishing.dao.ShopDao;
import com.afishing.dao.impl.CartDapImpl;
import com.afishing.dao.impl.ShopDaoImpl;
import com.afishing.entity.Shop;
import com.alibaba.fastjson.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "AddProductServlet", value = "/addProduct")
public class AddProductServlet extends HttpServlet {
    private final ShopDao  shopDao = new ShopDaoImpl();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");

        // 从请求中新增商品的数据
        String productJson = request.getReader().lines().reduce("", (accumulator, actual) -> accumulator + actual);

        //解析json字符串,将json字符串转换为Shop对象
        Shop product = JSONObject.parseObject(productJson, Shop.class);

        // 将数据插入数据库
        shopDao.addShop(product);

        // 返回响应给客户端
        response.getWriter().write("success");

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}